import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import { isLogin} from "@/utils/cookies";

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index')
      },{
        path: '/coll',
        name: 'coll',
        component: () => import('@/views/college/index')
      },{
        path: '/coll/satis',
        name: 'coll_satis',
        component: () => import('@/views/college/satisfaction')
      },{
        path: '/major',
        name: 'major',
        component: () => import('@/views/major/index')
      },{
        path: '/major/satis',
        name: 'major_satis',
        component: () => import('@/views/major/satisfaction')
      },{
        path: '/volun',
        name: 'volun',
        component: () => import('@/views/volun/index')
      },{
        path: '/volun/advisory',
        name: 'volun_advisory',
        component: () => import('@/views/volun/advisory')
      },{
        path: '/task',
        name: 'task',
        component: () => import('@/views/task/index')
      },{
          path: '/home/becomeCounselor',
        name: 'becomeCounselor',
        component: () => import('@/views/home/becomeCounselor')
      }
    ]
  },{
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },{
    path: '/reg',
    name: 'reg',
    component: () => import('@/views/register')
  },{
    path: '/user',
    name: 'user',
    component: () => import('@/views/user')
  },{
    path: '/forget',
    name: 'forget',
    component: () => import('@/views/forget')
  },{
    path: '/:catchAll(.*)',
    name: 'notfound',
    redirect: '/404'
  },{
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  }
]

const whitelist = ['/login', '/reg', '/forget', '/404', '/', '/home']

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (whitelist.indexOf(to.fullPath) != -1){
    next()
  }else {
    if (isLogin()){
      //已登录
      next();
    }else {
      //未登录
      next('/login')
    }
  }
})

export default router
